<?php
ini_set('date.timezone','Asia/Shanghai');
class SessionService
{
    const ExpireHour = 100; //过期时间

    /*
    *  插入memcache
    *
    */
    public static function addToMemcache($session_id,$session)
    {
        Util::log_outptut("登录成功 写入session ".$session_id);

        Yii::app()->cache->set($session_id,
            $session,
            0);

    }



    /*
    * 获取session对象
    *
    */
    public static function getObjFromMemcache($session_id)
    {

        $value = Yii::app()->cache->get($session_id);
        if(empty($value)==false)
            Util::log_outptut("memcache has session ".$session_id);
        return $value;


    }


    /*
    *
    *
    */
    public static function  getSessionObj($session_id)
    {

        //memcache获取session
        $session = SessionService::getObjFromMemcache($session_id);

        if(empty($session))
        {
            Util::log_outptut("memcache not has session ".$session_id);
            //表中获取session
            $session = GlSession::model()->findByPK($session_id);

        }

        return $session;


    }

    /*
     * 通过 user 生成 session
     *
     */

    public static function generateSession($user_id,$type)
    {
        $session_id = md5($user_id."key".DateUtil::GetCurrentUTCDateTime());
        $session = new GlSession();
        $session->session_id = $session_id;
        $session->user_id = $user_id;
        $session->create_time = DateUtil::GetCurrentUTCDateTime();
        $session->expire_time = DateUtil::getAfterNDayTime(SessionService::ExpireHour);
        $session->isVaild = 1;
        $session->type = $type;


        //插入
        SessionService::addToMemcache($session_id,$session);



        /*

        $r1= $session->save();

        if (!$r1) {

            //print_r($session->errors);

            throw new Exception("insert session error");
        }
        */

        return $session_id;


    }

    /*
     * 根据session 返回用户id
     */
    public  static function getUserIDBySessionid()
    {
        try {
            $session_id = Util::getParamNotEmpty("session_id");


            //memcache获取session
            $session = SessionService::getSessionObj($session_id);

            //session 过期/不存在

            if (empty($session)) {
                throw new Exception("session不存在");
            }

            //过期
            if (strtotime($session->expire_time) < strtotime(date("y-m-d h:i:s"))) {
                throw new Exception("session过期");

            }

            return $session->user_id;


        }catch(Exception $e)
        {
            throw $e;

        }


    }

    /*
     * 获取后台用户名
     */
    public static function getAdminUserName()
    {
        $user_id = SessionService::getUserIDBySessionid();

        $user = AdminUser::model()->findByPK($user_id);

        Util::checkExistAndNotDelete($user);

        return $user->user_name;
    }

    /*
     * 获取用户信息
     */
    public static function getUserInfo()
    {
        $session_id = Util::getParamNotEmpty("session_id");

        //session 过期/不存在
//        $session=GlSession::model()->findByPK($session_id);
//
//        Util::checkExistAndNotDelete($session);

        $session = SessionService::getSessionObj($session_id);

        $objid= $session->user_id;

        if($session->type == MsgConst::SESSION_TYPE_UNKNOWN)
            return "";

        if($session->type == MsgConst::SESSION_TYPE_USER)
        {

            return $objid;

        }

        if($session->type == MsgConst::SESSION_TYPE_WORKER)
        {

            return $objid;

        }

        if($session->type == MsgConst::SESSION_TYPE_ADMIN)
        {

            $obj = AdminUser::model()->findByPK($objid);

            Util::checkExistAndNotDelete($obj);

            return $obj->user_name;

        }

    }










}


?>